


from math import sqrt


N = 10000000 + 2
t = [0] * N
ds = [0] *N


def trans(x):
  ans = 0
  y = x
  while x != 0:
      ans += x % 10
      x = x // 10
  ds[y] = ans

def pre():
  for i in range(2,N):
    trans(i)
    
  N_ = int(sqrt(N))
  for i in range(2, N_):
    for k in range(2, N // i + 1):
        if ds[i] * ds[k] == ds[i * k]:
            t[i * k] = 1
            
            
  t[1]=t[0]=0
  for i in range(2,N):
    t[i]+=t[i-1]
      
  
def main():
  pre()
  T = int(input())
  for _ in range(T):
    L, R = map(int, input().split())
    print(t[R]-t[L-1])
    
    
if __name__ == "__main__":
  main()